package com.itheima.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.time.format.DateTimeFormatter;
import java.util.Date;

@WebServlet("/servletDemo01")
public class ServletDemo01 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=UTF-8");
        PrintWriter pw = resp.getWriter();
        pw.write("欢迎来到此界面，您最后一次访问时间为：");
//创建cookie对象，用于记录最后访问时间
Cookie cookie=new Cookie("time",System.currentTimeMillis()+"");
//设置最大存活时间
cookie.setMaxAge(3600);
//将cookie对象添加到客户端
resp.addCookie(cookie);
//获取cookie
        Cookie[] arr = req.getCookies();
        for (Cookie c : arr) {
            if ("time".equals(c.getName())){
                String value = c.getValue();
               SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                 pw.write("<br>");
                pw.write(sdf.format(Long.parseLong(value)));
            }

        }
        //得到value，并且写出


    }


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
